//
//  SuperviseDetailViewController.m
//  IphoneMapSdkDemo
//
//  Created by huck on 2017/5/22.
//  Copyright © 2017年 Baidu. All rights reserved.
//

#import "SuperviseDetailViewController.h"
#import "AppUtils.h"
#import "HomeManager.h"
#import "MyTabView.h"
#import "DealInfoView.h"
#import "WaterInfoView.h"
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件


@interface SuperviseDetailViewController ()<UITextFieldDelegate,BMKLocationServiceDelegate>

@property (nonatomic, strong) MyTabView *tabView;
@property (nonatomic) NSInteger currentIndex; //当前搜索类型

@property (nonatomic) BOOL isAdd; //是否是增加巡查

@property (nonatomic) BOOL infoLoading; //基础信息加载中标记
@property (nonatomic) BOOL searchLoading; //查询加载中标记

@property (nonatomic ,strong)WaterInfoView *infoView;
@property (nonatomic ,strong) DealInfoView *dealView;

@property (nonatomic,strong) BMKLocationService *locService; //

@end

@implementation SuperviseDetailViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubView];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [_infoView mapViewWillAppear];
    _locService.delegate = self;

    [self hideInput];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_infoView mapViewWillDisappear];
    _locService.delegate = nil;
    [self hideInput];
}

- (void)initSubView
{
    self.title = _waterName;
    _isAdd = [AppUtils isEmpty:_recordId];
    _currentIndex = 0;
    [HomeManager sharedInstance].waterDetailInfo = nil;
    if(!_isAdd){
        [HomeManager sharedInstance].waterRecordDetail = nil;
    }else{
        //    当前位置定位
        _locService = [[BMKLocationService alloc]init];
        //启动LocationService
        [_locService startUserLocationService];
    }
    
    [self initTabbarView];
    [self initTabView];
    [self onSelectTypeClick:@1];
}

//tab初始化
-(void)initTabbarView
{
    NSArray *names = @[@"基础信息",@"巡查督导"];
    NSInteger index = 1;
    _tabView = [MyTabView tabViewWithFrameView:_tabbarFrame];
    [_tabView setTabTitles:names font:[UIFont systemFontOfSize:14] selColor:[UIColor colorWithString:COLOR_17A4FF] unselColor:[UIColor colorWithString:COLOR_989AA6]];
    [_tabView setSelectionChangeHandler:self action:@selector(onSelectTypeClick:)];
    [_tabView setIndicatorColor:[UIColor colorWithString:COLOR_17A4FF]];
    [_tabView selectIndex:index];
    [_tabView updateFrameWidth:SCREEN_WIDTH];
}

//tab切换
- (void)onSelectTypeClick:(NSNumber*)number
{
    if (nil == number) {
        return;
    }
    NSInteger index = [number integerValue];
    if(_currentIndex != index){
        _currentIndex = index;
        _infoViewContainer.hidden = _currentIndex!=0;
        _dealViewContainer.hidden = _currentIndex==0;
    }
}

//初始化2个tab页面
-(void)initTabView
{
    _infoView = [WaterInfoView initViewWithFrameView:_infoViewContainer];
    _dealView = [DealInfoView initViewWithFrameView:_dealViewContainer delegate:self];
    
    if(![AppUtils hasConnected:YES]){
        return;
    }
    _dealView.waterId = _waterId;
    _dealView.recordId = _recordId;
//    _dealView.fieldDelegate = self;
    
    _infoLoading = YES;
    [Toast loading];
    [[HomeManager sharedInstance] requestWaterDetail:_waterId complete:^(BOOL success) {
        [_infoView updateInfo];
        _infoLoading = NO;
//        if(!_infoLoading && !_searchLoading){
//            [Toast loaded];
//        }
//        [_dealView updateInfo];
        
        //整改的话,
        if(!_isAdd){
            _searchLoading = YES;
            [[HomeManager sharedInstance] requestWaterRecodeDetail:_recordId complete:^(BOOL success) {
                _searchLoading = NO;
                    [Toast loaded];
                [_dealView updateInfo];
            }];
        }else{
            [Toast loaded];
            [_dealView updateInfo];
        }
        
    }];
}

//拍照图片
- (void)didFinishCropperImage:(UIImage *)image
{
    [_dealView didFinishCropperImage:image];
}


#pragma mark ========地图定位===[BMKLocationServiceDelegate]=======================
/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
   _dealView.myLocatoin = userLocation.location.coordinate;
    
}

// 用于自定义键盘的移动view （BaseViewController）
- (UIView *)frameRootView
{
    return _dealViewContainer;
}

@end
